﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DALTransit
/// </summary>
public class DALTransit : BaseDAL
{
    public DALTransit()
    {

    }

    public void InsertNewTransit(tblTransit transit)
    {
        db.tblTransits.InsertOnSubmit(transit);
        db.SubmitChanges();
    }

    public tblTransit GetTransit(int id)
    {
        return db.tblTransits.SingleOrDefault(c => c.ID == id);
    }

    public tblTransit GetTransit(int packageid, int branchid)
    {
        return db.tblTransits.FirstOrDefault(c => c.PackageID == packageid && c.BranchID == branchid);
    }

    public List<tblTransit> GetPackageTransits(int packageID)
    {
        List<tblTransit> lstTransit = new List<tblTransit>();
        tblTransit first_transit = GetFirstPackageTransit(packageID);
        if (first_transit != null)
        {
            tblTransit t = first_transit;
            lstTransit.Add(t);
            while (t.tblTransit1 != null)
            {
                t = t.tblTransit1;
                lstTransit.Add(t);
            };

        }
        return lstTransit;
        //return db.tblTransits.Where(c => c.PackageID == packageID).ToList();
    }

    public tblTransit GetFirstPackageTransit(int packageID)
    {
        return db.tblTransits.Where(c => c.PackageID == packageID).OrderBy(c => c.ID).FirstOrDefault();
    }

    public tblTransit GetLastPackageTransit(int packageID)
    {
        tblTransit first_transit = GetFirstPackageTransit(packageID);
        tblTransit t = null;
        if (first_transit != null)
        {
            t = first_transit;
            while (t.tblTransit1 != null)
            {
                t = t.tblTransit1;
            };
        }
        return t;
    }

    public vTransitRecord GetFirstPackagevTransit(int packageID)
    {
        return db.vTransitRecords.Where(c => c.PackageID == packageID).OrderBy(c => c.TransitID).FirstOrDefault();
    }

    //public vTransitRecord GetLastPackagevTransit(int packageID)
    //{
    //    return db.vTransitRecords.Where(c => c.PackageID == packageID).OrderBy(c => c.TransitID).LastOrDefault();
    //}

    public vTransitRecord GetvTransit(int transitid)
    {
        return db.vTransitRecords.Where(c => c.TransitID == transitid).SingleOrDefault();
    }

    public List<vTransitRecord> GetPackagevTransits(int packageID)
    {
        List<vTransitRecord> lstTransit = new List<vTransitRecord>();
        vTransitRecord first_transit = GetFirstPackagevTransit(packageID);
        if (first_transit != null)
        {
            vTransitRecord t = first_transit;
            lstTransit.Add(t);
            while (t.NextTransitID.HasValue && t.NextTransitID != 0)
            {
                t = GetvTransit(t.NextTransitID.Value);
                lstTransit.Add(t);
            };

        }
        return lstTransit;
        //return db.vTransitRecords.Where(c => c.PackageID == packageID).OrderBy(c => c.Date).ToList();
    }

    internal void Update()
    {
        db.SubmitChanges();
    }

    internal void DelTransit(int transitid)
    {
        db.tblTransits.DeleteOnSubmit(db.tblTransits.SingleOrDefault(c => c.ID == transitid));
        db.SubmitChanges();
    }

    internal tblTransit getPreTransit(int transitid)
    {
        return db.tblTransits.SingleOrDefault(c => c.NextTransitID == transitid);
    }
}